package com.beifu.manager.service.assetoutservice.impl;

import com.beifu.manager.api.common.PageInfo;
import com.beifu.manager.api.domain.warehouse.*;
import com.beifu.manager.api.domain.warehouse.washoutwarehousedetail.AssetOutList;
import com.beifu.manager.api.domain.warehouse.washoutwarehousedetail.Summary;
import com.beifu.manager.api.domain.wash.*;
import com.beifu.manager.common.Const;
import com.beifu.manager.dao.*;
import com.beifu.manager.model.*;
import com.beifu.manager.service.assetoutplanservice.AssetOutPlanService;
import com.beifu.manager.service.assetoutservice.AssetOutService;
import com.beifu.manager.service.assetouttruthservice.AssetOutTruthService;
import com.beifu.manager.service.organization.OrganizerService;
import com.beifu.manager.vo.assetout.*;
import com.mds.manager.utils.DateUtils;
import com.mds.manager.utils.PageUtils;
import com.utils.Constants;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: licy
 * @Date :  2018/6/17 10:48
 * @Last Modified by: licy
 * @Last Modified time:  2018/6/17 10:48
 */
@Service
public class AssetOutServiceImpl  implements AssetOutService{

    private Logger logger = Logger.getLogger(AssetOutServiceImpl.class);

    @Autowired
    private AssetOutMapper assetOutMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private OrganizerMapper organizerMapper;
    @Autowired
    private TrainInfoMapper trainInfoMapper;
    @Autowired
    private AssetOutPlanService assetOutPlanService;
    @Autowired
    private AssetOutTruthService assetOutTruthService;
    @Autowired
    private AssetOutPlanMapper assetOutPlanMapper;
    @Autowired
    private OrganizerService organizerService;
    @Autowired
    private AssetOutTruthMapper assetOutTruthMapper;
    @Autowired
    private  ImgMapper imgMapper;
    @Autowired
    private  AssetInfoMapper assetInfoMapper;
    @Autowired
    private  AssetBaseMapper assetBaseMapper;
    @Autowired
    private  AssetTypeMapper assetTypeMapper;

    /**
     * 出库单查询
     * @param page
     * @return
     */
    @Override
    public PageUtils getAssetOutPages(PageUtils page) {
//        SmsExample example = (SmsExample) page.getQueryParames();
        AssetOutExample  example = (AssetOutExample) page.getQueryParames();

        int count = assetOutMapper.countByExample(example);
        List<AssetOut> list = assetOutMapper.getAssetOutPages((page.getPage()-1)* page.getLimit(), page.getLimit(), example);


        List<AssetOutVo>  assetOutList = dealAssetOutList(list, false, false);
        page.setList(assetOutList);
        page.setCountNum(count);

        return page;
    }

    @Override
    public AssetOutDetailVo getAssetOutDetail(String assetOutId,boolean washFlag) {
        int integer = Integer.valueOf(assetOutId);
        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(integer);

        AssetOutDetailVo  detail = new AssetOutDetailVo();
        int status = assetOut.getStatus();
        if(Const.Status.FIN == status){
            detail = assetOutTruthService.getOneTruthDetail(assetOutId,washFlag,Const.Status.FIN );

        }else if(Const.Status.UNFIN == status){
            detail   = assetOutPlanService.getOnePlanDetail(assetOutId,washFlag);
            AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
            assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetOutId)).andSecondScanEqualTo(Const.Second_Scan.YES);
            int i = assetOutTruthMapper.countByExample(assetOutTruthExample);
            if(i == 0){
                List<AssetOutPlan> assetOutPlansList = detail.getAssetOutPlansList();
                for(AssetOutPlan assetOutPlan:assetOutPlansList){
                    assetOutPlan.setTruthCount(0);
                }
            }
        }else {
            logger.error(">>>.出库管理：有未知状态status："+status);
        }
        AssetOutVo assetOutVo = getAssetOutVo(assetOut, washFlag, true);
        detail.setAssetOutVo(assetOutVo);;
        return detail;

    }

    @Override
    public void createAsstOutOrder(String userId, String organizeId, String create_inTime, String status, List<String> assetTypeSelectArr, List<String> assetTypeNumArr, String create_remark, String destOrganizer, String userOrganizer) {
        //创建出库单
        AssetOut assetOut = new AssetOut();
        assetOut .setCreateTime(new Date());
        assetOut .setCreateUserId(Integer.valueOf(userId));
        assetOut .setSrcOrganizeId(Integer.valueOf(userOrganizer));
        assetOut .setDestOrganizeId(Integer.valueOf(destOrganizer));
        assetOut .setOutTime(com.mds.manager.utils.DateUtils.stringToDate(create_inTime, "yyyy-MM-dd"));
        assetOut .setStatus(Integer.valueOf(status));
        assetOut .setRemark(create_remark);
        assetOutMapper.insertAndGetId(assetOut);
        int assetOutId =  assetOut.getId();
        //保存备品入库单类型列表
        for(int i=0;i<assetTypeSelectArr.size();i++){
            String assetType = assetTypeSelectArr.get(i);
            String assetTypeNum = assetTypeNumArr.get(i);
            AssetOutPlan assetInPlan = new AssetOutPlan();
            assetInPlan.setAssetOutId(assetOutId);
            assetInPlan.setOutCount(Integer.valueOf(assetTypeNum));
            assetInPlan.setAssetTypeId(Integer.valueOf(assetType));
            assetOutPlanMapper.insert(assetInPlan);
        }
    }

    @Override
    public PageUtils getAssetOutPagesWithWashManage(PageUtils page, boolean washFlag, boolean isDetail) {
        AssetOutExample  example = (AssetOutExample) page.getQueryParames();

        int count = assetOutMapper.countByExample(example);
        List<AssetOut> list = assetOutMapper.getAssetOutPages((page.getPage()-1)* page.getLimit(), page.getLimit(), example);

        List<AssetOutVo>  assetOutList = dealAssetOutList(list,washFlag,isDetail);
        page.setList(assetOutList);
        page.setCountNum(count);
        return page;
    }



    /**
     * 包装出库展示列表数据
     * @param list
     * @param washFlag
     * @param isDetail
     * @return
     */
    private List<AssetOutVo> dealAssetOutList(List<AssetOut> list, boolean washFlag, boolean isDetail) {
        List<AssetOutVo> resultList = new ArrayList<AssetOutVo>();
        for (AssetOut assetOut:list){
            AssetOutVo assetOutVo = new AssetOutVo();
                 assetOutVo = getAssetOutVo(assetOut,washFlag ,isDetail);

            resultList.add(assetOutVo);
        }
        return  resultList;
    }

    private AssetOutVo getAssetOutVo(AssetOut assetOut, boolean washFlag, boolean isDetail) {
        AssetOutVo assetOutVo = new AssetOutVo();
        //出库单编号
        Integer assetOutId = assetOut.getId();
        String imgId = assetOut.getImgId();
        List<String> imgBase64List = new ArrayList<String>();
        if(isDetail){
            if(StringUtils.isNotBlank(imgId)){
                String[] split = imgId.split(",");
                for(int i = 0 ; i < split.length ; i++  ){
                    String imgIdpk = split[i].toString();

                    Img img = imgMapper.selectByPrimaryKey(Long.valueOf(imgIdpk));
                    imgBase64List.add(new String(img.getImgbase64()));
                }
            }
        }


        String assetOutIdStr = String.valueOf(assetOutId);
        //申请人 姓名
        Integer createUserId = assetOut.getCreateUserId();
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(createUserId);
        String userName = userInfo.getUserName();
        //订单时间
        Date createTime = assetOut.getCreateTime();
        String createTimeStr = DateUtils.getDateToStr(createTime);
        //送达时间
        Date outTime = assetOut.getOutTime();
        String outTimeStr = DateUtils.getDateToStr(outTime);
        //状态
        Integer status = assetOut.getStatus();
        //出库单位
        Integer organizeId = null;
        if(washFlag){
            //洗涤厂管理 ： 目标是自己
            organizeId   = assetOut.getDestOrganizeId();
        }else{
            //非洗涤厂 ：   源是自己
            organizeId   = assetOut.getSrcOrganizeId();
        }
        String  organizeName  = StringUtils.EMPTY;
        String  organizeIdStr = StringUtils.EMPTY;
        Organizer organizer = organizerMapper.selectByPrimaryKey(organizeId);
        if(organizeId!=null&&organizer!=null){
            organizeName           = organizer.getOrganizeName();
            organizeIdStr          = String.valueOf(organizeId);
        }
        //车辆编号
        Integer  trainId = assetOut.getTrainId();
        TrainInfo trainInfoReady = null;
        if(trainId!=null){
            trainInfoReady  = trainInfoMapper.selectByPrimaryKey(trainId);
        }
        String trainInfoId = null;
//        String trainNumber = StringUtils.EMPTY;

        String trainNo = StringUtils.EMPTY;
        if(trainInfoReady!=null ){
            Integer trainInfoIdint = trainInfoReady.getId();
//            trainNumber = trainInfoReady.getTrainNumber();
            trainInfoId = trainInfoIdint == null? StringUtils.EMPTY : String.valueOf(trainInfoIdint);
            trainNo = trainInfoReady.getTrainNo();
        }else {
            logger.error(">>获取出库数据：不存在为车次号为："+organizeIdStr);
        }
        assetOutVo.setImgBase64List(imgBase64List);
        assetOutVo.setAssetOutId(assetOutIdStr);     //出库单编号
        assetOutVo.setCreateUserName(userName);      //申请人 姓名
        assetOutVo.setCreateTime(createTimeStr);     //订单时间
        assetOutVo.setOutTime(outTimeStr);           //送达时间
        assetOutVo.setStatus(status);                //操作
        assetOutVo.setOrganizeName(organizeName);    //出库单位
        assetOutVo.setTrainId(trainNo);          //车辆编号
        assetOutVo.setTrainNumber(assetOut.getTrainTime());      //执行车次
        return assetOutVo;
    }

    @Override
    public PageInfo getOutWarehouseList(int userId, int pageIndex, int pageNumber) {

        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
        Integer organizeId = userInfo.getOrganizeId();

        AssetOutExample assetOutExample = new AssetOutExample();
        assetOutExample.createCriteria().andSrcOrganizeIdEqualTo(organizeId);
        int count = assetOutMapper.countByExample(assetOutExample);
        List<AssetOut> assetOutsList = assetOutMapper.getAssetOutPages((pageIndex-1)* pageNumber, pageNumber, assetOutExample);

        AssetsOutListApiVo assetsOutListApiVo = new AssetsOutListApiVo();
        List<AssetsOutApiVo> assetsOutApiVos = new ArrayList<AssetsOutApiVo>();
        for(AssetOut assetOut:assetOutsList){
            AssetsOutApiVo assetsOutApiVo = new AssetsOutApiVo();
            assetsOutApiVo.setAssetsOutId(assetOut.getId());
            assetsOutApiVo.setAssetsOutName(assetOut.getAssetName());
            assetsOutApiVo.setStatus(assetOut.getStatus());
            assetsOutApiVos.add(assetsOutApiVo);
        }

        PageInfo pageInfo = new PageInfo();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("assetsOutList", assetsOutApiVos);
        pageInfo.setResult(map);
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageNum(pageNumber);

        pageInfo.setHasNextPage(false);
        if (count > pageIndex*pageNumber) {
            pageInfo.setHasNextPage(true);
        }

        return pageInfo;
    }

    @Override
    public OutWarehouseDetailResponse getOutWarehouseDetail(Integer userIdInt, int assetsOutIdInt) {
        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(assetsOutIdInt);
        if(assetOut==null){
            return null ;
        }
        String trainTime = assetOut.getTrainTime();
        if(StringUtils.isBlank(trainTime)){
            trainTime  = "";
        }
        List<String> imgsListFromDB = null;
        if(StringUtils.isNotBlank(assetOut.getImgId())){
            imgsListFromDB = getImgsListFromDB(assetOut.getImgId());
        }

//      trainID	列车编号
        String trainNo = StringUtils.EMPTY;
        if(assetOut.getTrainId() !=null){
            int  trainId =assetOut.getTrainId() ;
            TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(trainId);
            trainNo =  trainInfo.getTrainNo();
        }



//      trainTime	执行车次
//        String trainNumber = trainInfo.getTrainNumber();
//      applyName	申请人
        Integer createUserId = assetOut.getCreateUserId();
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(createUserId);
        String userName = userInfo.getUserName();
//      applyDate	申请时间
        Date createTime = assetOut.getCreateTime();
        String applyDate = String.valueOf(createTime.getTime());
//      outCompany	出库单位
        Integer destOrganizeId = assetOut.getDestOrganizeId();
        Organizer organizer = organizerMapper.selectByPrimaryKey(destOrganizeId);
        String organizeName = organizer.getOrganizeName();
//      outCompanyType	出库单位类型
        Integer organizeType = organizer.getOrganizeType();
        Const.OrganizeTypeEnum organizeTypeEnum = Const.OrganizeTypeEnum.codeOf(organizeType);
        String outCompanyTypeName = organizeTypeEnum.getValue();
//      trainUnitList	车厢列表
        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        Long assetsOutIdLong = Long.valueOf(assetsOutIdInt);
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong);
        //        trainUnitID	车厢id
        //        num	        数量
        List<OutTrainUnit> outTrainUnitList = assetOutTruthMapper.selectTrainAndNmuByAssetOutId(assetsOutIdLong);

        OutWarehouseDetailResponse outWarehouseDetailResponse = new OutWarehouseDetailResponse();
        outWarehouseDetailResponse.setTrainId(trainNo);                          //列车编号
        outWarehouseDetailResponse.setTrainTime(trainTime);                    //执行车次
        outWarehouseDetailResponse.setApplaName(userName);                       //申请人
        outWarehouseDetailResponse.setApplyDate(applyDate);                      //申请时间
        outWarehouseDetailResponse.setOutCompany(organizeName);                  //出库单位
        outWarehouseDetailResponse.setOutCompanyTypeId(organizeType.toString());   //出库单位类型id
        outWarehouseDetailResponse.setOutCompanyTypeName(outCompanyTypeName);   //出库单位类型name
        outWarehouseDetailResponse.setOutCompanyId(destOrganizeId.toString());   //出库单位id


        outWarehouseDetailResponse.setImgList(imgsListFromDB);
        outWarehouseDetailResponse.setTrainUnitList(outTrainUnitList);           //车厢列表

        return outWarehouseDetailResponse;
    }

    @Override
    public Map<String, String> uploadOutWarehouse(JSONObject jsonIn, JSONObject jsonPub) {
        String userIdStr =     jsonPub.get("userId").toString();
        String devIDStr  =     jsonPub.get("devID").toString();
        String timestamp  = jsonPub.get("timestamp").toString();

        int userId = Integer.valueOf(userIdStr);
        String devID = devIDStr ;



//        String applyName = jsonIn.get("applyName").toString();
        String applyDate = jsonIn.get("applyDate").toString();
//        String outCompany = jsonIn.get("outCompany").toString();
        String outCompanyIdStr =  jsonIn.get("outCompanyId").toString();
//        String assetName =  jsonIn.get("assetName").toString();
        String assetName = "卧具出库单上传->洗涤厂";

        int outCompanyId = Integer.valueOf(outCompanyIdStr);
        //
        Object  trainTimeObj = jsonIn.get("trainTime") ;
        Object trainNoObj    = jsonIn.get("trainID") ;
        String trainTime = "";
        String trainNoStr  = "";
        Integer trainId = null;
        Integer destOrgId = null;
        boolean isHasTrainflag = false;
        if( trainTimeObj ==null || trainNoObj == null ){
            isHasTrainflag = false;
        }else {
            isHasTrainflag = true;
            trainTime  = trainTimeObj.toString();
            trainNoStr = trainNoObj.toString();
            TrainInfoExample trainInfoExample = new TrainInfoExample();
            trainInfoExample.createCriteria().andTrainNoEqualTo(trainNoStr);
            List<TrainInfo> trainInfos = trainInfoMapper.selectByExample(trainInfoExample);
            TrainInfo trainInfo = new TrainInfo();
            try {
                if (trainInfos != null && trainInfos.size() > 0) {
                    trainInfo = trainInfos.get(0);
                } else {
                    throw new RuntimeException("请传入正确的列车编号");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            trainId = trainInfo.getId();
            trainInfo.setTrainNumber(trainTime);       //执行车次
            trainInfoMapper.updateByPrimaryKey(trainInfo);
          //随机找一个洗涤厂
            OrganizerExample organizerExample = new OrganizerExample();
            organizerExample.createCriteria().andOrganizeTypeEqualTo(Const.Organize_Type.WASHING_FACTORY);
            List<Organizer> organizerList = organizerMapper.selectByExample(organizerExample);
            if(organizerList!=null && organizerList.size()>0){
                Organizer organizer = organizerList.get(0);
                destOrgId = organizer.getId();
            }
        }

//        String imgIdAll = StringUtils.EMPTY;
//        if( null!=jsonIn.get("imgList")){
//            String imgBase64ListStr = jsonIn.get("imgList").toString();
//
//            JSONArray json = JSONArray.fromObject(imgBase64ListStr);
//            List<ImgBase64> imgBase64List= (List<ImgBase64>)JSONArray.toCollection(json, ImgBase64.class);
//
//            if(imgBase64List!=null && imgBase64List.size()>0 ){
//                for (ImgBase64  imgBase64:imgBase64List){
//                    String imgBase64Img = imgBase64.getImg();
//                    Img img = new Img();
//                    img.setImgbase64(imgBase64Img.getBytes());
//                    imgMapper.insertAndGetId(img);
//                    Long id = img.getId();
//                    if(StringUtils.EMPTY.equals(imgIdAll)){
//                        imgIdAll = ""+ id;
//                    }else {
//                        imgIdAll = imgIdAll + ","+id ;
//                    }
//                }
//            }
////            imgIdAll = imgIdAll.substring(0,imgIdAll.length() - 1);
//        }



//        List<RfidWithCarriage> rfidList= (List<RfidWithCarriage>)jsonIn.get("RfidList");
        String rfidListJsonStr = jsonIn.get("rfidList").toString();
        JSONArray json = JSONArray.fromObject(rfidListJsonStr);
        List<RfidCarriageJson> rfidList= (List<RfidCarriageJson>)JSONArray.toCollection(json, RfidCarriageJson.class);


        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
        int organizeId = userInfo.getOrganizeId();

        Date applyDateD = new Date();
        applyDateD.setTime(Long.valueOf(applyDate));



        AssetOut assetOut = new AssetOut();
        if(isHasTrainflag){
            assetOut.setTrainId(trainId);
            assetOut.setTrainTime(trainTime);
            if (userInfo.getOrganize().getOrganizeType().equals(0)) {
            	assetOut.setDestOrganizeId(destOrgId);
			}else {
				assetOut.setDestOrganizeId(outCompanyId);
			}
        }else {
        	assetOut.setDestOrganizeId(outCompanyId);
        }

        assetOut.setCreateUserId(userId);
        assetOut.setAssetName(assetName);
        assetOut.setSrcOrganizeId(organizeId);
//        assetOut.setImgId(imgIdAll);
        assetOut.setCreateTime(applyDateD);
        assetOut.setOutTime(applyDateD);
        assetOut.setStatus(Const.Status.UNFIN);

        assetOutMapper.insertAndGetId(assetOut);

        int assetOutId = assetOut.getId();
        long assetOutIdLong = Long.valueOf(assetOutId);


        /**
         * 去重
         */
        Set<RfidCarriageJson> rfidSet = new HashSet(rfidList);


        List<AssetOutTruth>  assetOutTruthList = new ArrayList<AssetOutTruth>();

        for (RfidCarriageJson rfidCarriageJson:rfidSet){
            AssetOutTruth assetOutTruth = new AssetOutTruth();
            assetOutTruth.setInputTime(applyDateD);
            assetOutTruth.setOperatingDeviceId(devID);
            assetOutTruth.setOperatingUserId(userId);
            assetOutTruth.setAssetInfoRfidCode(rfidCarriageJson.getRfid());
            assetOutTruth.setCarriageCode(rfidCarriageJson.getTrainUnitID());
            assetOutTruth.setAssetOutId(assetOutIdLong);
            assetOutTruth.setFirstScan(Const.Tran_Status.YES);// 火车先扫
            assetOutTruth.setSecondScan(Const.Wash_Status.NO);// 洗涤厂后扫
            assetOutTruthList.add(assetOutTruth);
        }




        for(AssetOutTruth assetOutTruth:assetOutTruthList){
            assetOutTruthMapper.insertSelective(assetOutTruth);
        }

        //TODO LICY  正式环境需要去掉

        HashMap<Integer, Integer> hashMap = assetOutTruthService.getIntegerIntegerHashMap(assetOutTruthList);
        for (Map.Entry<Integer, Integer> entry : hashMap.entrySet()) {
            AssetOutPlan assetOutPlan = new AssetOutPlan();
            assetOutPlan.setAssetOutId(assetOutId);
            Integer assetTypeId = entry.getKey();
            Integer assetTruthCount = entry.getValue();
            assetOutPlan.setAssetTypeId(assetTypeId);
            assetOutPlan.setOutCount(assetTruthCount);

            assetOutPlanMapper.insertSelective(assetOutPlan);
        }
        Map<String ,String > map = new HashMap<String, String>();
        map.put("assetoutId",String.valueOf(assetOutIdLong));
        return map;

    }

    @Override
    public Summary getWashOutWarehouseDetail(int assetsOutId, int trainUnitId) {
        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(assetsOutId);
        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        long assetsOutIdLong = Long.valueOf(assetsOutId);
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong).andCarriageCodeEqualTo(String.valueOf(trainUnitId));
        List<AssetOutTruth> assetOutTruthList = assetOutTruthMapper.selectByExample(assetOutTruthExample);
        if(assetOutTruthList==null||assetOutTruthList.size()==0){
            throw new RuntimeException("该车厢无车厢无对应的rfid列表");
        }

        Map<String, Object> hashMap = assetOutTruthService.getRfIdwithHashMap(assetOutTruthList);
        HashMap<Integer,Integer> typeNumhashMap = (HashMap<Integer,Integer> )hashMap.get("typeNumhashMap");
        HashMap<Integer,List<String>>  typeRfidHashMap = (HashMap<Integer,List<String>> )hashMap.get("typeRfidHashMap");

        Summary summary = null;
        List<AssetOutList>  resultList = new ArrayList<AssetOutList>();
        List<WashTypeNum> washTypeNumList = new ArrayList<WashTypeNum>();

        for (Map.Entry<Integer, Integer> entry : typeNumhashMap.entrySet()) {
            WashTypeNum washTypeNum = new WashTypeNum();
            summary = new Summary();
            Integer assetTypeId = entry.getKey();
            Integer assetTruthCount = entry.getValue();
            washTypeNum.setWashNum(assetTruthCount.toString());

            AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
            washTypeNum.setWashType(assetTypeId.toString());
            washTypeNum.setWashTypeId(assetType.getTypeId());
            washTypeNum.setWashTypeModelName(assetType.getModelName());
            washTypeNum.setWashTypeName(assetType.getName());

            List<String> rfidList = typeRfidHashMap.get(assetTypeId);
            washTypeNumList.add(washTypeNum);
            for(String rfidStr:rfidList){
                AssetOutList assetOut1 = new AssetOutList();
                assetOut1.setRfid(rfidStr);
                assetOut1.setWashType(assetTypeId.toString());
                //FIXME
                assetOut1.setWashTypeId(assetType.getTypeId());
                assetOut1.setWashTypeName(assetType.getName());
                assetOut1.setWashTypeModelName(assetType.getModelName());
                resultList.add(assetOut1);
            }
            summary.setAssetoutlist(resultList);
        }
        summary.setWashTypeNumList(washTypeNumList);
        return summary;
    }

    @Override
    public PageInfo getAssetsWashList(Integer userIdInt, int pageIndex, int pageNumber) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userIdInt);
        Integer organizeId = userInfo.getOrganizeId();

        AssetOutExample assetOutExample = new AssetOutExample();
        assetOutExample.createCriteria().andDestOrganizeIdEqualTo(organizeId);
        assetOutExample.setOrderByClause("status asc");

        int count = assetOutMapper.countByExample(assetOutExample);
        List<AssetOut> assetOutsList = assetOutMapper.getAssetOutPages((pageIndex-1)* pageNumber, pageNumber, assetOutExample);

        List<AssetsOutApiVo> assetsOutApiVos = new ArrayList<AssetsOutApiVo>();
        for(AssetOut assetOut:assetOutsList){
            AssetsOutApiVo assetsOutApiVo = new AssetsOutApiVo();
            assetsOutApiVo.setAssetsWashId(assetOut.getId().toString());
            assetsOutApiVo.setAssetsWashName(assetOut.getAssetName());
            assetsOutApiVo.setStatus(assetOut.getStatus());
            assetsOutApiVos.add(assetsOutApiVo);
        }
        PageInfo pageInfo = new PageInfo();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("assetsOutList", assetsOutApiVos);
        pageInfo.setResult(map);
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageNum(pageNumber);

        pageInfo.setHasNextPage(false);
        if (count > pageIndex*pageNumber) {
            pageInfo.setHasNextPage(true);
        }

        return pageInfo;
    }

    @Override
    public AssetsWashDetailResponse getAssetsWashDetail(int assetWashId) {
        AssetsWashDetailResponse assetsWashDetailResponse = new AssetsWashDetailResponse();
        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(assetWashId);
        if(assetOut==null){
            return null;
        }


//      trainID	列车编号
        if(assetOut.getTrainId() != null){
            int  trainId =assetOut.getTrainId() ;
            TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(trainId);
            String trainNo = trainInfo.getTrainNo();
            assetsWashDetailResponse.setReceiveId(trainNo);                          //列车编号
            //执行车次
            String trainTime = assetOut.getTrainTime();
            if(StringUtils.isBlank(trainTime)){
                trainTime  = "1234";
            }
            assetsWashDetailResponse.setTrainTime(trainTime);                    //执行车次
        }

//      trainTime	执行车次
//        String trainNumber = trainInfo.getTrainNumber();
//      applyName	申请人
        Integer createUserId = assetOut.getCreateUserId();
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(createUserId);
        String userName = userInfo.getUserName();
//      applyDate	申请时间
        Date createTime = assetOut.getCreateTime();
        String applyDate = String.valueOf(createTime.getTime());
//      outCompany	出库类型
        Integer destOrganizeId = assetOut.getDestOrganizeId();
//        Integer srcOrganizeId = assetOut.getSrcOrganizeId();
        Organizer organizer = organizerMapper.selectByPrimaryKey(destOrganizeId);
        String organizeName = organizer.getOrganizeName();
//      outCompanyType	出库单位类型
        Integer organizeType = organizer.getOrganizeType();
        Integer organizerId = organizer.getId();
        Const.OrganizeTypeEnum organizeTypeEnum = Const.OrganizeTypeEnum.codeOf(organizeType);
        String srcCompanyTypeName = organizeTypeEnum.getValue();
//      trainUnitList	车厢列表
        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        Long assetsOutIdLong = Long.valueOf(assetWashId);
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong);
        //车        trainUnitID	车厢id
        //          num	        数量
        List<OutTrainUnit> outTrainUnitList = assetOutTruthMapper.selectTrainAndNmuByAssetOutId(assetsOutIdLong);
        // 洗涤厂       trainUnitID	车厢id
        //             num	        数量
        List<OutTrainUnit> outWashUnitList = assetOutTruthMapper.selectWashAndNmuByAssetOutId(assetsOutIdLong);
        //  应扫
        assetOutTruthExample.clear();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong).andFirstScanEqualTo(Const.Tran_Status.YES);
        int enableScan = assetOutTruthMapper.countByExample(assetOutTruthExample);
        //实际扫描
        assetOutTruthExample.clear();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong).andSecondScanEqualTo(Const.Tran_Status.YES);
        int actualScan = assetOutTruthMapper.countByExample(assetOutTruthExample);




        assetsWashDetailResponse.setAssetsOutType      (srcCompanyTypeName);   //出库单位类型
        assetsWashDetailResponse.setAssetsOutTypeId    (organizeType.toString());   //出库单位类型
        assetsWashDetailResponse.setAssetsOutTypeCpId(String.valueOf(organizerId));   //出库单位类型
        assetsWashDetailResponse.setAssetsOutTypeCpIdName(organizeName);   //出库单位类型


//        assetsWashDetailResponse.setTrainTime(trainTime);                    //执行车次
        assetsWashDetailResponse.setApplyName(userName);                       //申请人
        assetsWashDetailResponse.setApplyDate(applyDate);                      //申请时间
        assetsWashDetailResponse.setEnableScan(String.valueOf(enableScan));    //应扫描
        assetsWashDetailResponse.setActualScan(String.valueOf(actualScan));    //应扫描

        //外层车 内层洗涤厂
        List<WashProduct> washProductList = new ArrayList<WashProduct>();
        Iterator<OutTrainUnit> outTrainUnitListIt = outTrainUnitList.iterator();
        while (outTrainUnitListIt.hasNext()){
            OutTrainUnit outTrainUnitOut = outTrainUnitListIt.next();
//        for(OutTrainUnit outTrainUnitOut : outTrainUnitList){
            if(outTrainUnitList.size()>0) {
                for (OutTrainUnit outWashUnitIn : outWashUnitList) {
                    if (outTrainUnitOut.getTrainUnitID().equals(outWashUnitIn.getTrainUnitID())) {
                        WashProduct washProduct = new WashProduct();
                        washProduct.setTrainUnitId(outTrainUnitOut.getTrainUnitID());
                        washProduct.setEnableScan(outTrainUnitOut.getNum());
                        washProduct.setActualScan(outWashUnitIn.getNum());
                        washProductList.add(washProduct);
                        outTrainUnitListIt.remove();
                    }
                }
            }

        }

        if(outTrainUnitList.size()>0){
            for(OutTrainUnit outTrainUnitOut : outTrainUnitList){
                WashProduct washProduct = new WashProduct();
                washProduct.setTrainUnitId(outTrainUnitOut.getTrainUnitID());
                washProduct.setEnableScan(outTrainUnitOut.getNum());
                washProduct.setActualScan("0");
                washProductList.add(washProduct);
            }

        }




        assetsWashDetailResponse.setTrainUnitList(washProductList);           //洗涤单列表
        return assetsWashDetailResponse;
    }

    @Override
    public WashDetailSummary getWashDetailSubDetail(String assetWashId, String trainUnitId) {
        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetWashId)).andCarriageCodeEqualTo(trainUnitId);

        //是否完成 flag
//        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetWashId)).andSecondScanEqualTo(Const.Wash_Status.YES);
//        int i = assetOutTruthMapper.countByExample(assetOutTruthExample);
//
//        assetOutTruthExample.clear();
//        boolean isFinFlag = i > 0 ? true : false;
//        boolean notBlank = StringUtils.isNotBlank(trainUnitId);
//        if(isFinFlag && notBlank){
//            assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetWashId)).andCarriageCodeEqualTo(trainUnitId).andSecondScanEqualTo(Const.Wash_Status.YES).andFirstScanEqualTo(Const.First_Scan.YES);
//        }else if (!isFinFlag && notBlank){
//            assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetWashId)).andCarriageCodeEqualTo(trainUnitId).andFirstScanEqualTo(Const.First_Scan.YES);
//        }else if ( isFinFlag && !notBlank){
//            assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetWashId)).andSecondScanEqualTo(Const.Wash_Status.YES).andFirstScanEqualTo(Const.First_Scan.YES);
//        }else if ( !isFinFlag && !notBlank){
//            assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetWashId)).andFirstScanEqualTo(Const.First_Scan.YES);
//        }

//        Integer outTruthStatus = outTruth.getStatus();
//        assetOutTruthExample.clear();
//        if(StringUtils.isNotBlank(trainUnitId)){
//        }else {
//            assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetWashId)).andSecondScanEqualTo(Const.Wash_Status.YES);;
//        }



        List<AssetOutTruth> assetOutTruthList = assetOutTruthMapper.selectByExample(assetOutTruthExample);

        WashDetailSummary summary = new WashDetailSummary();

        List<WashTypeNum> washTypeNumList = new ArrayList<WashTypeNum>();
        HashMap<Integer, Integer> hashMap = assetOutTruthService.getIntegerIntegerHashMap(assetOutTruthList);
        for (Map.Entry<Integer, Integer> entry : hashMap.entrySet()) {
            WashTypeNum washTypeNum = new WashTypeNum();
            Integer assetTypeId = entry.getKey();
            Integer assetTruthCount = entry.getValue();
            AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
            washTypeNum.setWashNum(assetTruthCount.toString());
            washTypeNum.setWashType(assetTypeId.toString());
            washTypeNum.setWashTypeId(assetType.getTypeId());
            washTypeNum.setWashTypeName(assetType.getName());
            washTypeNum.setWashTypeModelName(assetType.getModelName());
            washTypeNumList.add(washTypeNum);
        }
        summary.setWashTypeNumList(washTypeNumList);

        List<AssetWashDetail> assetWashList = new ArrayList<AssetWashDetail>();
        for(AssetOutTruth assetOutTruth : assetOutTruthList){
            AssetWashDetail assetWashDetail = new AssetWashDetail();
            String assetInfoRfidCode = assetOutTruth.getAssetInfoRfidCode();
            AssetInfoExample assetInfoExample = new AssetInfoExample();
            assetInfoExample.createCriteria().andRfidCodeEqualTo(assetInfoRfidCode);
            List<AssetInfo> assetInfos = assetInfoMapper.selectByExample(assetInfoExample);
            if(assetInfos!=null && assetInfos.size()>0) {
                AssetInfo assetInfo = assetInfos.get(0);
                assetOutTruth.setStatus(assetInfo.getStatus());
                Integer assetBaseid = assetInfo.getAssetBaseid();
                AssetBase assetBase = assetBaseMapper.selectByPrimaryKey(assetBaseid);
                Integer assetTypeId = assetBase.getAssetTypeId();
                AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
//                Integer status = assetInfo.getStatus();

                assetWashDetail.setRFID(assetInfoRfidCode);
                assetOutTruthExample.clear();
                assetOutTruthExample.createCriteria().andAssetInfoRfidCodeEqualTo(assetInfoRfidCode).andAssetOutIdEqualTo(Long.valueOf(assetWashId)).andCarriageCodeEqualTo(trainUnitId).andSecondScanEqualTo(Const.Wash_Status.YES).andFirstScanEqualTo(Const.First_Scan.YES);
                int i = assetOutTruthMapper.countByExample(assetOutTruthExample);
                if(i>0){
                    assetWashDetail.setStatus(String.valueOf(Const.Status.FIN));
                }else {
                    assetWashDetail.setStatus(String.valueOf(Const.Status.UNFIN));
                }

                assetWashDetail.setWashType(assetType.getId().toString());
                assetWashDetail.setWashTypeId(assetType.getTypeId());
                assetWashDetail.setWashTypeName(assetType.getName());
                assetWashDetail.setWashTypeModleName(assetType.getModelName());

                assetWashList.add(assetWashDetail);

            }
        }
        summary.setAssetWashList(assetWashList);
        return summary;
    }

    @Override
    public WashDetailSummary uploadwashscanDetail(List<RfIdOnly> rfIdOnlyList, String userId, String devID, String assetWashId, String timestamp) {

        AssetOut out = assetOutMapper.selectByPrimaryKey(Integer.valueOf(assetWashId));
        if(Const.Status.FIN==out.getStatus()){
            throw new RuntimeException("该订单已完成，不能再次提交");
        }

        List<AssetOutTruth> assetOutTruthList = new ArrayList<AssetOutTruth>();
        List<AssetInfo> assetInfoList = new ArrayList<AssetInfo>();

        for(RfIdOnly rfIdOnly:rfIdOnlyList){
            String rfid = rfIdOnly.getRFID();
            AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
            assetOutTruthExample.createCriteria().andAssetInfoRfidCodeEqualTo(rfid).andAssetOutIdEqualTo(Long.valueOf(assetWashId));
            List<AssetOutTruth> assetOutTruthList1 = assetOutTruthMapper.selectByExample(assetOutTruthExample);
            AssetOutTruth  assetOutTruth =null;
            if(assetOutTruthList1!=null&&assetOutTruthList1.size()>0){
                assetOutTruth=  assetOutTruthList1.get(0);
            }else {
                continue;
            }
            Date date = new Date();
            date.setTime(Long.valueOf(timestamp));
            assetOutTruth.setSecondInputTime(date); //       洗涤厂后扫的时间
            assetOutTruth.setAssetInfoRfidCode(rfid);
            assetOutTruth.setSecondDeviceId(devID);
            assetOutTruth.setSecondOperationId(Integer.valueOf(userId));
            assetOutTruth.setSecondScan(Const.Wash_Status.YES); //洗涤厂后扫
            AssetInfoExample assetInfoExample = new AssetInfoExample();
            assetInfoExample.createCriteria().andRfidCodeEqualTo(rfid);
            List<AssetInfo> infoList = assetInfoMapper.selectByExample(assetInfoExample);

            assetOutTruthList.add(assetOutTruth);

            AssetInfo assetInfo = new AssetInfo();
            if(infoList!=null && infoList.size()>0) {
                assetInfo = infoList.get(0);
                Integer washNum = assetInfo.getWashNum();
                if(washNum==null ){
                    assetInfo.setWashNum(+1);
                }else {
                    assetInfo.setWashNum(washNum+1);
                }
//                assetInfo.setStatus(Integer.valueOf(Constants.ASSET_INFO_STATUS.InWash.getStatus()));
            }else {
                assetInfo.setRfidCode(rfid);
                Integer washNum = assetInfo.getWashNum();
                if(washNum==null ){
                    assetInfo.setWashNum(+1);
                }else {
                    assetInfo.setWashNum(washNum+1);
                }
//                assetInfo.setStatus(Integer.valueOf(Constants.ASSET_INFO_STATUS.InWash.getStatus()));
            }
            assetInfo.setStatus(Integer.valueOf(Constants.ASSET_INFO_STATUS.InWash.getStatus()));
            assetInfoList.add(assetInfo);
        }

        /*
         *  修改未扫描状态 为在车
         */
        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetWashId)).andSecondScanEqualTo(Const.Second_Scan.NO);
        List<AssetOutTruth> assetOutTruthListInCar = assetOutTruthMapper.selectByExample(assetOutTruthExample);
        for(AssetOutTruth assetOutTruth:assetOutTruthListInCar){
            AssetInfoExample assetInfoExample = new AssetInfoExample();
            assetInfoExample.createCriteria().andRfidCodeEqualTo(assetOutTruth.getAssetInfoRfidCode());
            List<AssetInfo> assetInfoList1 = assetInfoMapper.selectByExample(assetInfoExample);
            if(assetInfoList1!=null&&assetInfoList1 .size()>0){
                AssetInfo assetInfo = assetInfoList1.get(0);
                assetInfo.setStatus(Integer.valueOf(Constants.ASSET_INFO_STATUS.InCar.getStatus()));
                assetInfoMapper.updateByPrimaryKeySelective(assetInfo);
            }
        }







//        assetInfoMapper.updateBatch(assetInfoList);
        for(AssetInfo assetInfo:assetInfoList){
            if(assetInfo.getId()==null){
//                assetInfo.setAssetBaseid(1);
//                assetInfoMapper.insertSelective(assetInfo);
                throw new RuntimeException("没有找到对应的rfid");
            }else{
                assetInfoMapper.updateByPrimaryKey(assetInfo);
            }
        }
        for(AssetOutTruth assetOutTruth:assetOutTruthList){
            assetOutTruthMapper.updateByPrimaryKey(assetOutTruth);
        }

        //处理订单状态
//        boolean statusBoolean = dealAssetOutStatus(assetWashId);

        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(Integer.valueOf(assetWashId));
//        if(statusBoolean){
            assetOut.setStatus(Const.Status.FIN);
//        }else{
//            assetOut.setStatus(Const.Status.UNFIN);
//        }
        assetOutMapper.updateByPrimaryKey(assetOut);

        return null;
    }

    private boolean dealAssetOutStatus(String assetOutId) {


//        List<AssetOutPlan> planList = assetOutPlanService.dealAssetOutStatus(assetOutId);
//        for(AssetOutPlan assetOutPlan:planList){
//            if(assetOutPlan.getOutCount()!= assetOutPlan.getTruthCount() ){
//                return false;
//            }
//        }
        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetOutId)).andSecondScanEqualTo(Const.Second_Scan.NO);
        int i = assetOutTruthMapper.countByExample(assetOutTruthExample);
        if( i > 0 ){
            return false;
        }
        return true;
    }

    @Override
    public PageInfo getInWarehouseList(Integer userIdInt, int pageIndex, int pageNumber) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userIdInt);
        Integer organizeId = userInfo.getOrganizeId();

        AssetOutExample assetOutExample = new AssetOutExample();
        assetOutExample.createCriteria().andDestOrganizeIdEqualTo(organizeId);
        int count = assetOutMapper.countByExample(assetOutExample);
        List<AssetOut> assetOutsList = assetOutMapper.getAssetOutPages((pageIndex-1)* pageNumber, pageNumber, assetOutExample);

        List<InWarehouseResponse> list = new ArrayList<InWarehouseResponse>();
        for(AssetOut assetOut:assetOutsList){
            InWarehouseResponse inWarehouseResponse = new InWarehouseResponse();
            inWarehouseResponse.setAssetsInId(String.valueOf(assetOut.getId()));
            inWarehouseResponse.setAssetsInName(assetOut.getAssetName());
            inWarehouseResponse.setStatus(String.valueOf(assetOut.getStatus()));
            list.add(inWarehouseResponse);
        }
        PageInfo pageInfo = new PageInfo();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("assetsInList", list);
        pageInfo.setResult(map);
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageNum(pageNumber);

        pageInfo.setHasNextPage(false);
        if (count > pageIndex*pageNumber) {
            pageInfo.setHasNextPage(true);
        }

        return pageInfo;
    }

    @Override
    public InWarehouseDetailResponse getInWarehouseDetail(Integer userIdInt, int assetsOutIdInt) {

        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(assetsOutIdInt);
        if(assetOut==null){
            throw new RuntimeException("该订单不存在！");
        }
        String trainTime = StringUtils.EMPTY;
        String trainNo = StringUtils.EMPTY;

        if(StringUtils.isNotBlank(assetOut.getTrainTime())&&null != assetOut.getTrainId() ){
            trainTime = assetOut.getTrainTime();
            if(StringUtils.isBlank(trainTime)){
                trainTime = "" ;
            }
            //      trainID	列车编号
            int  trainId = assetOut.getTrainId();
            TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(trainId);
            trainNo= trainInfo.getTrainNo();
        }

        List<String> imgsListFromDB = null;
        if(StringUtils.isNotBlank(assetOut.getImgId())){
            imgsListFromDB = getImgsListFromDB(assetOut.getImgId());
        }


//      trainTime	执行车次
//        String trainNumber = trainInfo.getTrainNumber();
//      applyName	申请人
        Integer createUserId = assetOut.getCreateUserId();
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(createUserId);
        String userName = userInfo.getUserName();
//      applyDate	申请时间
        Date createTime = assetOut.getCreateTime();
        String applyDate = String.valueOf(createTime.getTime());
//      outCompany	出库单位
        Integer destOrganizeId = assetOut.getDestOrganizeId();
        Organizer organizer = null;
        try{
            organizer = organizerMapper.selectByPrimaryKey(destOrganizeId);
            String organizeName = organizer.getOrganizeName();
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("没有找到对应的目标组织");
        }

//      outCompanyType	出库单位类型
        Integer organizeType = organizer.getOrganizeType();
        Const.OrganizeTypeEnum organizeTypeEnum = Const.OrganizeTypeEnum.codeOf(organizeType);
        String outCompanyTypeName = organizeTypeEnum.getValue();

        AssetOutPlanExample assetOutPlanExample = new AssetOutPlanExample();
        assetOutPlanExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdInt);
        List<AssetOutPlan> assetOutPlanList = assetOutPlanMapper.selectByExample(assetOutPlanExample);

        InWarehouseDetailResponse scrap = new InWarehouseDetailResponse();
        scrap.setTrainId(trainNo);                          //列车编号
        scrap.setTrainTime(trainTime);                    //执行车次
        scrap.setApplaName(userName);                       //申请人
        scrap.setApplyDate(applyDate);                      //申请时间
        scrap.setImgList(imgsListFromDB);
        List<InWarehouseDetail> inWarehouseDetailList = new ArrayList<InWarehouseDetail>();
        for(AssetOutPlan assetOutPlan:assetOutPlanList){
            InWarehouseDetail inWarehouseDetails = new InWarehouseDetail();
            inWarehouseDetails.setType(assetOutPlan.getAssetTypeId().toString());
            inWarehouseDetails.setNum(assetOutPlan.getOutCount().toString());
            AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetOutPlan.getAssetTypeId());

            inWarehouseDetails.setTypeName(assetType.getName());
            inWarehouseDetails.setTypeModelName(assetType.getModelName());
            inWarehouseDetails.setTypeId(assetType.getTypeId());
            inWarehouseDetailList.add(inWarehouseDetails);
        }
        scrap.setApplyDetail(inWarehouseDetailList);           //车厢列表
        return scrap;
    }

    @Override
    public Integer upInWarehouseDetail(JSONObject jsonIn, JSONObject jsonPub) {
        String  trainNoStr  = jsonIn.get("trainId").toString();
        TrainInfoExample trainInfoExample = new TrainInfoExample();
        trainInfoExample.createCriteria().andTrainNoEqualTo(trainNoStr);
        List<TrainInfo> trainInfos = trainInfoMapper.selectByExample(trainInfoExample);
        TrainInfo trainInfo = new TrainInfo();
        if(trainInfos != null && trainInfos.size()>0){
            trainInfo = trainInfos.get(0);
        }else {
            throw  new RuntimeException("请传入正确的列车编号");
        }

        Integer trainId = trainInfo.getId();
//        int trainId  = Integer.valueOf(trainIdStr);
        String  trainTime  = jsonIn.get("trainTime").toString();
        trainInfo.setTrainNumber(trainTime);       //执行车次

        trainInfoMapper.updateByPrimaryKey(trainInfo);
//        String  assetName  = jsonIn.get("assetName").toString();
//        String  applaName  = jsonIn.get("applaName").toString();
        String  applyDate  = jsonIn.get("applyDate").toString();
        String  applyDetailListStr  = jsonIn.get("applyDetail").toString();

        JSONArray json = JSONArray.fromObject(applyDetailListStr);
        List<InWarehouseDetail> applyDetailList= (List<InWarehouseDetail>)JSONArray.toCollection(json, InWarehouseDetail.class);



        String userId    =    jsonPub.get("userId").toString();
        String devID     =    jsonPub.get("devID").toString();
        String timestamp =    jsonPub.get("timestamp").toString();

        Integer userIdInt = Integer.valueOf(userId);

        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userIdInt);
        Integer organizeId = userInfo.getOrganizeId();
        //随机找一个洗涤厂
        OrganizerExample organizerExample = new OrganizerExample();
        organizerExample.createCriteria().andOrganizeTypeEqualTo(Const.Organize_Type.WASHING_FACTORY);
        List<Organizer> organizerList = organizerMapper.selectByExample(organizerExample);
        Integer srcOrganizerId = null;
        if(organizerList!=null && organizerList.size()>0){
            Organizer organizer = organizerList.get(0);
            srcOrganizerId = organizer.getId();
        }

        AssetOut assetOut = new AssetOut();
        assetOut.setCreateUserId(userIdInt);
        assetOut.setAssetName("入库单上传");
        assetOut.setTrainId(trainId);
        //目标自己 源默认
        assetOut.setSrcOrganizeId(srcOrganizerId);
        assetOut.setDestOrganizeId(organizeId);
//        assetOut.setImgId(imgIdAll);
        Date date = new Date();
        date.setTime(Long.valueOf(applyDate));
        assetOut.setCreateTime(date);
        assetOut.setOutTime(date);
        assetOut.setStatus(Const.Status.UNFIN);
        assetOut.setTrainTime(trainTime);
        assetOutMapper.insertAndGetId(assetOut);

        Integer assetOutId = assetOut.getId();

        for(InWarehouseDetail inWarehouseDetail:applyDetailList){

            AssetOutPlan assetOutPlan = new AssetOutPlan();
            int type = Integer.valueOf(inWarehouseDetail.getType());
            AssetType assetType = assetTypeMapper.selectByPrimaryKey(type);
            if(assetType==null){
               throw new RuntimeException("该类型未维护");
            }
            assetOutPlan.setAssetTypeId(type);
            assetOutPlan.setOutCount(Integer.valueOf(inWarehouseDetail.getNum()));
            assetOutPlan.setAssetOutId(assetOutId);

            assetOutPlanMapper.insertSelective(assetOutPlan);
        }
        return assetOutId;
    }

    @Override
    public void  upInWarehouseRfidDetail(JSONObject jsonIn, JSONObject jsonPub) {



        String userId = jsonPub.get("userId").toString();
        String devID = jsonPub.get("devID").toString();
        String timestamp = jsonPub.get("timestamp").toString();

        String assetsInId = jsonIn.get("assetsInId").toString();

        //校验用户第一次是否扫描


//        List<RfIdOnly> rfIdOnlyList = ( List<RfIdOnly>)jsonIn.get("rfidList");
        String rfIdOnlyListStr = jsonIn.get("rfidList").toString();

        JSONArray json = JSONArray.fromObject(rfIdOnlyListStr);
        List<RfIdOnly> rfIdOnlyList= (List<RfIdOnly>)JSONArray.toCollection(json, RfIdOnly.class);

        List<AssetOutTruth> assetOutTruthList = new ArrayList<AssetOutTruth>();
        List<AssetInfo> assetInfoList = new ArrayList<AssetInfo>();

        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(Integer.valueOf(assetsInId));


        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetsInId)).andFirstScanEqualTo("1");
        int byExample = assetOutTruthMapper.countByExample(assetOutTruthExample);
        if(byExample ==  0){
            throw new RuntimeException("车入库第一次未扫描，请先进行第一次扫描。");
        }
        //判断订单是否已经完成
        //处理订单状态  如果是已经完成的状态则不需要抛出异常
        dealAssetOutStatusOut(assetsInId);

        Set<RfIdOnly> onlySet = new HashSet<RfIdOnly>(rfIdOnlyList);

        //车入库扫描 上传的rfidList
        for(RfIdOnly rfIdOnly:onlySet){
            String rfid = rfIdOnly.getRFID();
//            AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
            assetOutTruthExample.clear();
            assetOutTruthExample.createCriteria().andAssetInfoRfidCodeEqualTo(rfid).andAssetOutIdEqualTo(Long.valueOf(assetsInId));
            //如果有重复的id跳过
            List<AssetOutTruth> assetOutTruthList1 = assetOutTruthMapper.selectByExample(assetOutTruthExample);
            AssetOutTruth  assetOutTruth =null;
            if(assetOutTruthList1!=null&&assetOutTruthList1.size()>0){
                assetOutTruth=  assetOutTruthList1.get(0);
            }else {
                continue;
            }
            Date newDate = new Date();
            newDate.setTime(Long.valueOf(timestamp));
            assetOutTruth.setSecondInputTime(newDate);
            assetOutTruth.setAssetInfoRfidCode(rfid);
            assetOutTruth.setSecondDeviceId(devID);
            assetOutTruth.setSecondOperationId(Integer.valueOf(userId));
//            assetOutTruth.setWashScan(Const.Wash_Status.YES);
            assetOutTruth.setSecondScan(Const.Tran_Status.YES);      //车入库 ： 车后扫
            AssetInfoExample assetInfoExample = new AssetInfoExample();
            assetInfoExample.createCriteria().andRfidCodeEqualTo(rfid);
            List<AssetInfo> assetInfos = assetInfoMapper.selectByExample(assetInfoExample);

            assetOutTruthList.add(assetOutTruth);

            if(assetInfos!=null && assetInfos.size()>0) {
                AssetInfo assetInfo = assetInfos.get(0);
                Integer useNum = assetInfo.getUseNum();
                if(useNum == null ){
                    useNum = 1 ;
                }
                assetInfo.setUseNum(useNum + 1);
                assetInfo.setStatus(Integer.valueOf(Constants.ASSET_INFO_STATUS.InCar.getStatus()));
                assetInfoList.add(assetInfo);
            }else {
                AssetInfo assetInfo = new AssetInfo();
                assetInfo.setUseNum(1);
                assetInfo.setRfidCode(rfid);
                assetInfo.setStatus(Integer.valueOf(Constants.ASSET_INFO_STATUS.InCar.getStatus()));
                assetInfoList.add(assetInfo);

            }
        }//FIXME
         assetOutTruthExample.clear();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetsInId)).andSecondScanEqualTo(Const.Second_Scan.NO);
        List<AssetOutTruth> assetOutTruthListInCar = assetOutTruthMapper.selectByExample(assetOutTruthExample);
        for(AssetOutTruth assetOutTruth:assetOutTruthListInCar){
            AssetInfoExample assetInfoExample = new AssetInfoExample();
            assetInfoExample.createCriteria().andRfidCodeEqualTo(assetOutTruth.getAssetInfoRfidCode());
            List<AssetInfo> assetInfoList1 = assetInfoMapper.selectByExample(assetInfoExample);
            if(assetInfoList1!=null&&assetInfoList1 .size()>0){
                AssetInfo assetInfo = assetInfoList1.get(0);

                Organizer organizer = organizerMapper.selectByPrimaryKey(assetOut.getSrcOrganizeId());
                Integer organizeType = organizer.getOrganizeType();
                Const.OrganizeTypeEnum organizeTypeEnum = Const.OrganizeTypeEnum.codeOf(organizeType);
                int value = organizeTypeEnum.getCode();
                assetInfo.setStatus(value );
                assetInfoMapper.updateByPrimaryKeySelective(assetInfo);
            }
        }




//        if(statusBoolean){
//            assetOut.setStatus(Const.Status.FIN);
//        }else{
//            assetOut.setStatus(Const.Status.UNFIN);
//        }
        assetOut.setStatus(Const.Status.FIN);
        assetOutMapper.updateByPrimaryKey(assetOut);


//        assetInfoMapper.updateBatch(assetInfoList);
        for(AssetInfo assetInfo:assetInfoList){
            if(assetInfo.getId()==null){
//                assetInfo.setAssetBaseid(1);
//                assetInfoMapper.insertSelective(assetInfo);
                throw new RuntimeException("基础信息表中无对应的rfid");
            }else{
                assetInfoMapper.updateByPrimaryKey(assetInfo);
            }
        }
        for(AssetOutTruth assetOutTruth:assetOutTruthList){
            assetOutTruthMapper.updateByPrimaryKey(assetOutTruth);
        }


    }

    private void dealAssetOutStatusOut(String assetsInId) {
//        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
//        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetsInId)).andSecondScanEqualTo(Const.Second_Scan.NO);
//        int i = assetOutTruthMapper.countByExample(assetOutTruthExample);
//        if( i > 0 ){
//            return false;
//        }
//        return true;

        AssetOutExample assetOutExample = new AssetOutExample();
        assetOutExample.createCriteria().andIdEqualTo(Integer.valueOf(assetsInId));
        int i = assetOutMapper.countByExample(assetOutExample);
        if(i==0){
            throw new RuntimeException("该订单不存在，不能上传");
        }
        assetOutExample.clear();
        assetOutExample.createCriteria().andIdEqualTo(Integer.valueOf(assetsInId)).andStatusEqualTo(Const.Status.FIN);
        i = assetOutMapper.countByExample(assetOutExample);
        if(i>0){
            throw new RuntimeException("该订单已经完成，不能再次上传");
        }

    }

    @Override
    public void upOutWashInWarehouseRfidDetail(JSONObject jsonIn, JSONObject jsonPub) {



        String userId = jsonPub.get("userId").toString();
        String devID = jsonPub.get("devID").toString();
        String timestamp = jsonPub.get("timestamp").toString();

        String assetsInId = jsonIn.get("assetsInId").toString();

        //判断订单状态 ： 如果是已完成的状态则不能上传 抛出异常
        dealAssetOutStatusOut(assetsInId);


        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetsInId)).andFirstScanEqualTo("1");
        int byExample = assetOutTruthMapper.countByExample(assetOutTruthExample);
        if(byExample > 0){
            throw new RuntimeException("车入库第一次扫描已经完成，不能再次扫描。");
        }

//        List<RfIdOnly> rfIdOnlyList = ( List<RfIdOnly>)jsonIn.get("rfidList");
        String rfIdOnlyListStr = jsonIn.get("rfidList").toString();

        JSONArray json = JSONArray.fromObject(rfIdOnlyListStr);

        List<RfIdOnly> rfIdOnlyListOrg= (List<RfIdOnly>)JSONArray.toCollection(json, RfIdOnly.class);

        Set<RfIdOnly> rfIdOnlyList = new HashSet<RfIdOnly>(rfIdOnlyListOrg);


        List<AssetOutTruth> assetOutTruthList = new ArrayList<AssetOutTruth>();
        List<AssetInfo> assetInfoList = new ArrayList<AssetInfo>();

        for(RfIdOnly rfIdOnly:rfIdOnlyList){
            String rfid = rfIdOnly.getRFID();
//            AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
//            assetOutTruthExample.createCriteria().andAssetInfoRfidCodeEqualTo(rfid).andAssetOutIdEqualTo(Long.valueOf(assetsInId));
//            List<AssetOutTruth> assetOutTruthList1 = assetOutTruthMapper.selectByExample(assetOutTruthExample);
            AssetOutTruth  assetOutTruth = new AssetOutTruth();
//            if(assetOutTruthList1!=null&&assetOutTruthList1.size()>0){
//                assetOutTruth=  assetOutTruthList1.get(0);
//            }else {
//                continue;
//            }
            Date newDate = new Date();
            newDate.setTime(Long.valueOf(timestamp));
            assetOutTruth.setAssetOutId(Long.valueOf(assetsInId));
            assetOutTruth.setInputTime(newDate);              //车入库 ： 洗涤厂先扫 时间
            assetOutTruth.setAssetInfoRfidCode(rfid);
            assetOutTruth.setOperatingDeviceId(devID);
            assetOutTruth.setOperatingUserId(Integer.valueOf(userId));
            assetOutTruth.setFirstScan(Const.Wash_Status.YES);  //车入库 ： 洗涤厂先扫
            assetOutTruth.setSecondScan(Const.Tran_Status.NO);   //车入库 ： 车后扫
            AssetInfoExample assetInfoExample = new AssetInfoExample();
            assetInfoExample.createCriteria().andRfidCodeEqualTo(rfid);
            List<AssetInfo> assetInfos = assetInfoMapper.selectByExample(assetInfoExample);

            assetOutTruthList.add(assetOutTruth);

            if(assetInfos!=null && assetInfos.size()>0) {
                AssetInfo assetInfo = assetInfos.get(0);
//                assetInfo.setStatus(Integer.valueOf(Constants.ASSET_INFO_STATUS.InWash.getStatus()));
                assetInfoList.add(assetInfo);
            }else {
                AssetInfo assetInfo = new AssetInfo();
                assetInfo.setRfidCode(rfid);
//                assetInfo.setStatus(Integer.valueOf(Constants.ASSET_INFO_STATUS.InWash.getStatus()));
                assetInfoList.add(assetInfo);

            }
        }
//        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(Integer.valueOf(assetsInId));
//        assetOut.setStatus(Const.Status.FIN);

//        assetOutMapper.updateByPrimaryKey(assetOut);
//        assetInfoMapper.updateBatch(assetInfoList);
        for(AssetInfo assetInfo:assetInfoList){

            //FIXME 获取更新 扫描到的状态
            AssetOut assetOut = assetOutMapper.selectByPrimaryKey(Integer.valueOf(assetsInId));
            Organizer organizer = organizerMapper.selectByPrimaryKey(assetOut.getSrcOrganizeId());
            Integer organizeType = organizer.getOrganizeType();
            Const.OrganizeTypeEnum organizeTypeEnum = Const.OrganizeTypeEnum.codeOf(organizeType);
            Integer value = organizeTypeEnum.getCode();
            assetInfo.setStatus(value);

            if(assetInfo.getId()==null){
//                assetInfo.setAssetBaseid(1);
//                assetInfoMapper.insertSelective(assetInfo);
                throw new RuntimeException("没有找到对应的rfid");
            }else{
                assetInfoMapper.updateByPrimaryKey(assetInfo);
            }
        }

        for(AssetOutTruth assetOutTruth:assetOutTruthList){
            assetOutTruthExample.clear();
            assetOutTruthExample.createCriteria().andAssetInfoRfidCodeEqualTo(assetOutTruth.getAssetInfoRfidCode()).andAssetOutIdEqualTo(Long.valueOf(assetsInId));
            List<AssetOutTruth> assetOutTruthList1 = assetOutTruthMapper.selectByExample(assetOutTruthExample);
            if(assetOutTruthList1!=null &&assetOutTruthList1.size()>0){
                AssetOutTruth assetOutTruth1 = assetOutTruthList1.get(0);
                assetOutTruth.setId(assetOutTruth1.getId());
                assetOutTruthMapper.updateByPrimaryKeySelective(assetOutTruth);
            }else {
                assetOutTruthMapper.insertSelective(assetOutTruth);
            }
        }
        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(Integer.valueOf(assetsInId));


//        if(statusBoolean){
//            assetOut.setStatus(Const.Status.FIN);
//        }else{
//            assetOut.setStatus(Const.Status.UNFIN);
//        }

//        assetOut.setStatus(Const.Status.FIN);
        assetOutMapper.updateByPrimaryKey(assetOut);

    }

    @Override
    public AssetsWashDetailResponse getOutWashInWarehouseRfidDetailFin(int assetInId) {
        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(assetInId);
        if(assetOut==null){
            return  null;
        }
        String trainTime = assetOut.getTrainTime();
        if(StringUtils.isBlank(trainTime)){
            trainTime  = "1234";
        }
//      trainID	列车编号
        int  trainId = assetOut.getTrainId();
        TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(trainId);
        String trainNo = trainInfo.getTrainNo();
//      trainTime	执行车次
//        String trainNumber = trainInfo.getTrainNumber();
//      applyName	申请人
        Integer createUserId = assetOut.getCreateUserId();
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(createUserId);
        String userName = userInfo.getUserName();
//      applyDate	申请时间
        Date createTime = assetOut.getCreateTime();
        String applyDate = String.valueOf(createTime.getTime());
//      outCompany	出库类型
        Integer destOrganizeId = assetOut.getDestOrganizeId();
        Organizer organizer = organizerMapper.selectByPrimaryKey(destOrganizeId);
        String organizeName = organizer.getOrganizeName();
//      outCompanyType	出库单位类型
        Integer organizeType = organizer.getOrganizeType();
        Const.OrganizeTypeEnum organizeTypeEnum = Const.OrganizeTypeEnum.codeOf(organizeType);
        String outCompanyTypeName = organizeTypeEnum.getValue();
//      trainUnitList	车厢列表
        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        Long assetsOutIdLong = Long.valueOf(assetInId);
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong);
        //车        trainUnitID	车厢id
        //          num	        数量
        List<OutTrainUnit> outTrainUnitList = assetOutTruthMapper.selectTrainAndNmuByAssetOutId(assetsOutIdLong);
        // 洗涤厂       trainUnitID	车厢id
        //             num	        数量
        List<OutTrainUnit> outWashUnitList = assetOutTruthMapper.selectWashAndNmuByAssetOutId(assetsOutIdLong);
        //   实际扫描
        assetOutTruthExample.clear();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong).andSecondScanEqualTo(Const.Tran_Status.YES);
        int  actualScan = assetOutTruthMapper.countByExample(assetOutTruthExample);
        //    应扫
        assetOutTruthExample.clear();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong).andFirstScanEqualTo(Const.Tran_Status.YES);
        int  enableScan = assetOutTruthMapper.countByExample(assetOutTruthExample);


        //

        AssetsWashDetailResponse assetsWashDetailResponse = new AssetsWashDetailResponse();
        assetsWashDetailResponse.setAssetsOutType(organizeType.toString());   //出库单位类型
        assetsWashDetailResponse.setReceiveId(trainNo);                        //列车编号
        assetsWashDetailResponse.setTrainTime(trainTime);                    //执行车次
        assetsWashDetailResponse.setApplyName(userName);                       //申请人
        assetsWashDetailResponse.setApplyDate(applyDate);                      //申请时间
        assetsWashDetailResponse.setEnableScan(String.valueOf(enableScan));    //应扫描
        assetsWashDetailResponse.setActualScan(String.valueOf(actualScan));    //实际扫描


        assetOutTruthExample.clear();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong).andSecondScanEqualTo(Const.Tran_Status.YES);
        List<AssetOutTruth> assetOutTruthList = assetOutTruthMapper.selectByExample(assetOutTruthExample);


        Map<String, Object> hashMap = assetOutTruthService.getRfIdwithHashMap(assetOutTruthList);
        HashMap<Integer,Integer> typeNumhashMap = (HashMap<Integer,Integer> )hashMap.get("typeNumhashMap");
        HashMap<Integer,List<String>>  typeRfidHashMap = (HashMap<Integer,List<String>> )hashMap.get("typeRfidHashMap");

        List<AssetOutList>  resultList = new ArrayList<AssetOutList>();
        List<WashTypeNum> washTypeNumList = new ArrayList<WashTypeNum>();
        for (Map.Entry<Integer, Integer> entry : typeNumhashMap.entrySet()) {
            WashTypeNum washTypeNum = new WashTypeNum();
            Integer assetTypeId = entry.getKey();
            Integer assetTruthCount = entry.getValue();
            washTypeNum.setWashNum(assetTruthCount.toString());
            washTypeNum.setWashType(assetTypeId.toString());
            AssetOutPlanExample assetOutPlanExample = new AssetOutPlanExample();
            assetOutPlanExample.createCriteria().andAssetOutIdEqualTo(assetInId).andAssetTypeIdEqualTo(assetTypeId);
            List<AssetOutPlan> assetOutPlanList = assetOutPlanMapper.selectByExample(assetOutPlanExample);
            if(assetOutPlanList !=null && assetOutPlanList.size() >0 ){
                AssetOutPlan assetOutPlan = assetOutPlanList.get(0);
                Integer planOutCount = assetOutPlan.getOutCount();
                washTypeNum.setPlanNum(planOutCount.toString());
            }
            List<String> rfidList = typeRfidHashMap.get(assetTypeId);
            washTypeNumList.add(washTypeNum);
            for(String rfidStr:rfidList){
                AssetOutList assetOut1 = new AssetOutList();
                assetOut1.setRfid(rfidStr);
                assetOut1.setWashType(assetTypeId.toString());

                //FIXME
                AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
                assetOut1.setWashTypeId(assetType.getTypeId());
                assetOut1.setWashTypeName(assetType.getName());
                assetOut1.setWashTypeModelName(assetType.getModelName());

                resultList.add(assetOut1);
            }
        }

        if(assetOutTruthList==null ||assetOutTruthList.size() == 0){
            List<WashTypeNum> washTypeNumList2 = new ArrayList<WashTypeNum>();
            AssetOutPlanExample assetOutPlanExample = new AssetOutPlanExample();
            assetOutPlanExample.createCriteria().andAssetOutIdEqualTo(assetInId);
            List<AssetOutPlan> assetOutPlanList = assetOutPlanMapper.selectByExample(assetOutPlanExample);

            for(AssetOutPlan assetOutPlan:assetOutPlanList){
                WashTypeNum washTypeNum = new WashTypeNum();
                Integer planOutCount = assetOutPlan.getOutCount();
                Integer assetTypeId = assetOutPlan.getAssetTypeId();
                washTypeNum.setPlanNum(planOutCount.toString());
                washTypeNum.setWashType(assetTypeId.toString());
                //FIXME
                AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
                washTypeNum.setWashTypeId(assetType.getTypeId());
                washTypeNum.setWashTypeName(assetType.getName());
                washTypeNum.setWashTypeModelName(assetType.getModelName());

                washTypeNumList2.add(washTypeNum);
            }

            assetsWashDetailResponse.setWashTypeNumList(washTypeNumList2);
        }else {
            assetsWashDetailResponse.setWashTypeNumList(washTypeNumList);
        }
        
        assetsWashDetailResponse.setResultList(resultList);

        return assetsWashDetailResponse;
    }


    @Override
    public PageUtils getWashByPages(PageUtils page) {
        HashMap reqMap  = (HashMap) page.getQueryParames();
        // 组织类型为 洗涤厂
        reqMap.put("organizeType", "2");

        if (null != reqMap.get("searchName") && null != reqMap.get("searchType")) {
            String searchName = reqMap.get("searchName").toString();
            String searchType = reqMap.get("searchType").toString();
            // 季度 格式201801、201802、201803、201804
            if (searchType.equals("1") && searchName.length() == 6){
                reqMap.put("searchName",searchName.substring(5,6));
                reqMap.put("yearName",searchName.substring(0,4));
            }

        }

        int count = assetOutMapper.countWashReport(reqMap);
        List<WashReportVo> list = assetOutMapper.selectWashReportByPage((page.getPage()-1)* page.getLimit(), page.getLimit(), reqMap);



        if (list != null && list.size()>0) {
            for (WashReportVo washReportVo:list) {
                int i = assetOutTruthMapper.countNumByAssetOutId(washReportVo.getAssetOutId());
                if (i == 0) {
                    logger.error("=======查询出库单"+washReportVo.getAssetOutId()+"出库资产数量为0=========");
                    break;
                }
                washReportVo.setWashNum(String.valueOf(i));
            }
        }

        OrganizerExample organizerExample = new OrganizerExample();
        organizerExample.createCriteria().andOrganizeTypeEqualTo(2);
        List<Organizer> washList = organizerMapper.selectByExample(organizerExample);

        List<WashReportVo> voList = new ArrayList<WashReportVo>();
        if (washList != null && washList.size()>0) {
            for (Organizer organizer : washList) {
                WashReportVo washReportVo = new WashReportVo();
                washReportVo.setWashName(organizer.getOrganizeName());
                boolean flag = false;
                for (WashReportVo record:list) {
                    if (organizer.getOrganizeName().equals(record.getWashName())) {
                        Long temp = StringUtils.isBlank(washReportVo.getWashNum()) ? 0 : Long.valueOf(washReportVo.getWashNum());
                        washReportVo.setWashNum(String.valueOf(temp+Long.valueOf(record.getWashNum())));
                        flag = true;
                    }
                }
                if (flag) {
                    voList.add(washReportVo);
                }
            }
        }

        page.setList(voList);
        page.setCountNum(count);

        return page;
    }

    @Override
    public AssetOutDetailVo getAssetOutDetailOut(String assetOutId, boolean washFlag) {
        int integer = Integer.valueOf(assetOutId);
        AssetOut assetOut = assetOutMapper.selectByPrimaryKey(integer);

        AssetOutDetailVo  detail = new AssetOutDetailVo();
        int status = assetOut.getStatus();
        if(Const.Status.FIN == status){
                detail = assetOutTruthService.getOneTruthDetailOut(assetOutId,washFlag,Const.Status.FIN );

        }else if(Const.Status.UNFIN == status){
            detail   = assetOutPlanService.getOnePlanDetailOut(assetOutId,washFlag);
            AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
            assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetOutId)).andSecondScanEqualTo(Const.Second_Scan.YES);
            int i = assetOutTruthMapper.countByExample(assetOutTruthExample);
            if(i == 0){
                List<AssetOutPlan> assetOutPlansList = detail.getAssetOutPlansList();
                for(AssetOutPlan assetOutPlan:assetOutPlansList){
                    assetOutPlan.setTruthCount(0);
                }
            }
        }else {
            logger.error(">>>.出库管理：有未知状态status："+status);
        }
        AssetOutVo assetOutVo = getAssetOutVo(assetOut, washFlag, true);
        detail.setAssetOutVo(assetOutVo);;
        return detail;
    }

    @Override
    public void batchDelAssetOut(String[] idsArr) {
        assetOutMapper.batchDelAssetOut( idsArr);
    }

    @Override
    public void upInTrainerScanPaperDetail(JSONObject jsonIn, JSONObject jsonPub) {

        String userId = jsonPub.get("userId").toString();
        String devID = jsonPub.get("devID").toString();
        String timestamp = jsonPub.get("timestamp").toString();

        String assetsInId = jsonIn.get("assetsInId").toString();


        String imgBase64ListStr = jsonIn.get("imgList").toString();
        String imgIdAll = StringUtils.EMPTY;
        JSONArray json = JSONArray.fromObject(imgBase64ListStr);
        List<ImgBase64> imgBase64List= (List<ImgBase64>)JSONArray.toCollection(json, ImgBase64.class);
        if(imgBase64List!=null && imgBase64List.size()>0 ){
            for (ImgBase64  imgBase64:imgBase64List){
                String imgBase64Img = imgBase64.getImg();
                Img img = new Img();
                img.setImgbase64(imgBase64Img.getBytes());
                imgMapper.insertAndGetId(img);
                Long id = img.getId();
                if(StringUtils.EMPTY.equals(imgIdAll)){
                    imgIdAll = ""+ id;
                }else {
                    imgIdAll = imgIdAll + ","+id ;
                }
            }
        }
        AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
        assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetsInId)).andFirstScanEqualTo("1");
//        int byExample = assetOutTruthMapper.countByExample(assetOutTruthExample);
//        if(byExample ==  0){
//            throw new RuntimeException("车入库第一次未扫描，请先进行第一次扫描。");
//        }
        //判断订单是否已经完成
        //处理订单状态  如果是已经完成的状态则不需要抛出异常
        dealAssetOutStatusOut(assetsInId);
        AssetOutTruth assetOutTruth = new AssetOutTruth();
        assetOutTruth.setSecondScan(Const.Second_Scan.YES);
        assetOutTruth.setSecondOperationId(Integer.valueOf(userId));
        assetOutTruth.setSecondInputTime(new Date());
        assetOutTruth.setSecondDeviceId(devID);
        AssetOutTruthExample outTruthExample = new AssetOutTruthExample();
        outTruthExample.createCriteria().andAssetOutIdEqualTo(Long.valueOf(assetsInId));
        assetOutTruthMapper.updateByExampleSelective(assetOutTruth,outTruthExample);
        AssetOut assetOut = new AssetOut();
        assetOut.setImgId(imgIdAll);
        assetOut.setId(Integer.valueOf(assetsInId));
        assetOut.setStatus(Const.Status.FIN);
        assetOutMapper.updateByPrimaryKeySelective( assetOut);

    }


    public static boolean isPureDigital(String string) {
        if (StringUtils.isBlank(string))
            return false;
        String regEx1 = "\\d+";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(string);
        if (m.matches())
            return true;
        else
            return false;
    }

    private List<String> getImgsListFromDB(String images) {
        List<String> imgList = new ArrayList<String>();

        if(StringUtils.isNotEmpty(images)){
            String[] imgIds = images.split(",");
            for (String imgId : imgIds) {
                Img img = imgMapper.selectByPrimaryKey(Long.parseLong(imgId));
                if(null != img){
                    imgList.add(new String(img.getImgbase64()));
                }
            }
        }

        return imgList;
    }
}